<%= form_for @user_story, url: @url do |f| %>

	<% if flash[:error].any? %>
	    <div class="error_messages">
			<h2>Beim absenden des Formulars sind folgende Fehler aufgetreten</h2>
			<ul>
				<% for message in flash[:error].full_messages %>
					<li><%= message %></li>
				<% end %>
			</ul>
	    </div>
	<% end %>
	<h3>Informationen</h3>
	<table class="userStoryTable">
		<tr>
			<td>Erstellt von:</td>
			<td><input type="text" readonly="readonly" value="<%= User.find_by_id(@user_story.user_pid).name %>"></td>
		</tr>
		<tr>
			<td>User Story ID:</td>
			<td><input type="text" id="user_story_id" name="user_story[id]" readonly="readonly" value="<%= @user_story.id %>"></td>
		</tr>
		<tr>
			<td>Projekt:</td>
			<td><input type="text" id="project" name="user_story[product_backlog_pid]" readonly="readonly" value="<%= Project.find_by_id(ProductBacklog.find_by_id(@user_story.product_backlog_pid).project_pid).name %>"></td>
		</tr>
	</table>
	<h3>User Story</h3>
	<table>
		<tr>
			<td>&Uuml;berschrift</td>
			<td><%= f.text_field :headline %></td>
		</tr>
		<tr>
			<td>Beschreibung</td>
			<td><%= f.text_area :description %></td>
		</tr>
	</table>
	<h3>Akzeptanzkriterien</h3>
	<table>
		<tr>
			<td>Vollverb</td>
			<td><%= f.text_field :verb %> </td>
		</tr>
		<tr>
			<td>Substantiv</td>
			<td><%= f.text_field :noun %> </td>
		</tr>
		<tr>
			<td colspan="2" class="center bold orange">
				<p>Fragenkatalog</p>
			</td>
		</tr>
                <% Questionnaire.all.each_with_index do |questionnaire, i| %>
                  <% unless @user_story.verb == nil and @user_story.noun == nil %>
                    <% questionnaire.question.gsub!(/<#verb>/, "<span class='mainVerb'>"+@user_story.verb+"</span>") %>
                    <% questionnaire.question.gsub!(/<#noun>/, "<span class='mainNoun'>"+@user_story.noun+"</span>") %>
                  <% else %>
                    <% questionnaire.question.gsub!(/<#verb>/, "<span class='mainVerb'><#verb></span>") %>
                    <% questionnaire.question.gsub!(/<#noun>/, "<span class='mainNoun'><#noun></span>") %>
                  <% end %>
		<tr>
			<td><%= raw questionnaire.question %></td>
                        <td>
                            <%= text_area_tag 'user_story[answers_to_questionnaires_attributes]['+i.to_s+'][answer]', @answers[i]==nil ? "" : @answers[i].answer, :class => "answer" %>
                            <%=
                              unless @answers[i].nil?
                                hidden_field_tag "user_story[answers_to_questionnaires_attributes]["+i.to_s+"][id]", @answers[i].id
                              end
                            %>
                        </td>
		</tr>
                <% end %>
		<tr>
			<td colspan="2" class="center bold orange">
				<p>Daraus resultierende Akzeptanzkreterien</p>
			</td>
		</tr>
		<tr>
			<td colspan="2">
				<table class="acceptabilityCriterion">
                                  <% Criterion.find_all_by_user_story_id(@user_story.id).each_with_index do |criterions, i| %>
					<tr>
						<td><div><%= text_area_tag 'user_story[criterions_attributes]['+i.to_s+'][criterion]', @criterions[i]==nil ? "" : @criterions[i].criterion, :class => "criterion" %></div></td>
						<td><div><input type='checkbox' id="user_story_criterions_attributes_<%= i.to_s %>_complied" name="user_story[criterions_attributes][<%= i.to_s %>][complied]" <%= "checked" if  criterions.complied %>> </div></td>
                                                <td><div id="deleteCriterion">
                                                    <a href="javascript:void(null)">
                                                        <img alt="Dieses Akzeptanzkriterium l&amp;ouml;schen" class="icon" id="eraseIcon" src="/assets/layout/Erase.png">
                                                    </a>
                                                    <%=
                                                      unless @answers[i].nil?
                                                        hidden_field_tag "user_story[criterions_attributes]["+i.to_s+"][id]", @criterions[i].id
                                                      end
                                                    %>
                                                </div></td>
					</tr>
                                  <% end %>
				</table>
				<input name="new_criterion" id ="new_criterion" type="button" value="Add new">
			</td>
		</tr>
	</table>	
	<h3>Einstellungen</h3>
	<table>
		<tr>
			<td>Priorität</td>
			<td><%= select("user_story", "user_story_priority_pid", UserStoryPriority.all.collect {|u| [u.priority, u.id]}, {include_blank: '--- Bitte wählen ---'}) %></td>
		</tr>
		<tr>
			<td>Status</td>
			<td><%= select("user_story", "user_story_state_pid", UserStoryState.find_all_by_id([1, 2, 4]).collect {|u| [raw(u.status), u.id]}, {include_blank: '--- Bitte wählen ---'}) %></td>
		</tr>
	</table>
  
    <center><%= f.submit %></center>
    
<% end %>
